05. Array Index
Indexing
Remember that elements in an array are indexed starting at the position 0
. To access an element in an array, use the name of the array immediately followed by square brackets containing the index of the value you want to access.
var donuts = ["glazed", "powdered", "sprinkled"];
console.log(donuts[0]); // "glazed" is the first element in the `donuts` array
Prints: "glazed"
One thing to be aware of is if you try to access an element at an index that does not exist, a value of undefined will be returned back.
console.log(donuts[3]); // the fourth element in `donuts` array does not exist!
Prints: undefined
Using Array Indices
SOLUTION:
donuts[6]Finally, if you want to change the value of an element in array, you can do so by setting it equal to a new value.
donuts[1] = "glazed cruller"; // changes the second element in the `donuts` array to "glazed cruller"
console.log(donuts[1]);
Prints: "glazed cruller"
Changing Value at an Index
INSTRUCTOR NOTE:
Change the value of an element by setting it equal to a new value.
What does the array look like?